package com.chuangjiangx.member.manager.client.web.stored.controller;

import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.request.Page;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.commons.response.UnderlinePageResponse;
import com.chuangjiangx.member.manager.basic.web.controller.BaseController;
import com.chuangjiangx.member.manager.basic.web.interceptor.Login;
import com.chuangjiangx.member.manager.basic.web.interceptor.Permissions;
import com.chuangjiangx.member.manager.client.web.stored.request.SearchStoredInfoRequest;
import com.chuangjiangx.member.manager.client.web.stored.request.StoredDetailForClientRequest;
import com.chuangjiangx.member.manager.client.web.stored.response.ClientStoredDetailResponse;
import com.chuangjiangx.member.manager.client.web.stored.response.StoredDetailInfoForClientResponse;
import com.chuangjiangx.member.manager.common.Constants;
import com.chuangjiangx.member.manager.common.ThreadLocalUser;
import com.chuangjiangx.member.stored.ddd.dal.condition.QueryClientStoredListCondition;
import com.chuangjiangx.member.stored.ddd.query.MbrStoredStreamClientQuery;
import com.chuangjiangx.member.stored.ddd.query.dto.ClientStoredStreamListDTO;
import com.chuangjiangx.member.stored.ddd.query.dto.StoredStreamInfoForClientDTO;
import com.sun.jmx.snmp.ThreadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 会员储值明细（移动端APP）
 */
@RestController
@Api(value = "会员储值明细（移动端APP）",tags = "储值")
@RequestMapping(value = "/app/member/personalStored", produces = "application/json")
public class MbrStoredClientController extends BaseController {
    @Autowired
    private  MbrStoredStreamClientQuery mbrStoredStreamClientQuery;

    /**
     * 储值明细列表--商户权限
     *
     * @param clientRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "查询指定会员的储值明细列表--商户权限")
    @RequestMapping(value = "/searchStoredDetailList",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("55003")
    public UnderlinePageResponse searchStoredDetailListForClient(@Validated @RequestBody StoredDetailForClientRequest clientRequest, HttpServletRequest request) {

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);
        QueryClientStoredListCondition condition = new QueryClientStoredListCondition();
        BeanUtils.convertBean(clientRequest, condition);
        condition.setMemberId(clientRequest.getMemberId());
        condition.setMerchantId(user.getMerchantId());
        //获取分页信息
        Page page = new Page();
        page.setPageNO(clientRequest.getPageNumber());
        page.setEveryPageCount(clientRequest.getPageSize());

        PagingResult<ClientStoredStreamListDTO> pagingResult = mbrStoredStreamClientQuery.queryClientStoredStreamList(condition);
        List<ClientStoredDetailResponse> respList = BeanUtils.convertCollection(pagingResult.getItems(), ClientStoredDetailResponse.class, null);

        return (UnderlinePageResponse)ResponseUtils.successPage(page, pagingResult, "memberStoredList", respList);
    }


    /**
     * 储值明细列表--门店权限
     *
     * @param clientRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "查询指定会员的储值明细列表--门店权限")
    @RequestMapping(value = "/searchStoreStoredList",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("56003")
    public UnderlinePageResponse searchStoredDetailListForClientStore(@Validated @RequestBody StoredDetailForClientRequest clientRequest, HttpServletRequest request) {

        ThreadLocalUser user = (ThreadLocalUser) ThreadContext.get(Constants.CURRENT_LOGIN_USER_KEY);

        QueryClientStoredListCondition condition = new QueryClientStoredListCondition();
        BeanUtils.convertBean(clientRequest, condition);
        condition.setMemberId(clientRequest.getMemberId());
        condition.setMerchantId(user.getMerchantId());
        condition.setStoreId(user.getStoreId());

        //获取分页信息
        Page page = new Page();
        page.setPageNO(clientRequest.getPageNumber());
        page.setEveryPageCount(clientRequest.getPageSize());

        PagingResult<ClientStoredStreamListDTO> pagingResult = mbrStoredStreamClientQuery.queryClientStoredStreamList(condition);
        List<ClientStoredDetailResponse> respList = BeanUtils.convertCollection(pagingResult.getItems(), ClientStoredDetailResponse.class, null);

        return (UnderlinePageResponse)ResponseUtils.successPage(page, pagingResult, "memberStoredList", respList);
    }


    /**
     * 根据储值明细id查询储值明细详情
     */
    @ApiOperation(value = "根据储值明细id查询储值明细详情")
    @RequestMapping(value = "/searchStoredDetail",method = RequestMethod.POST)
    @ResponseBody
    @Login
    @Permissions("59017")
    public Response<StoredDetailInfoForClientResponse> searchStoredDetailInfo(@Validated @RequestBody SearchStoredInfoRequest request) {
        StoredDetailInfoForClientResponse response = new StoredDetailInfoForClientResponse();
        StoredStreamInfoForClientDTO clientDTO = mbrStoredStreamClientQuery.searchStoredStreamInfoForClient(request.getId());
        BeanUtils.convertBean(clientDTO, response);
        return ResponseUtils.success("memberStoredInfo", response);
    }

}
